﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using XC.RSAUtil;

namespace Naruto.TaskManagement.Infrastructure
{
    public static class RSAHelper
    {
        private static string Prk = $"<RSAKeyValue><Modulus>0EEdKBvBFu2b1zhNmmIJu/eHGVmaTdq+G1ahZBiVh4zSYQHaFv7h3jcx9pAu63RiiRMcaapkuKEmGSx38ci1QSnqjWLg3ACDgqjofF9a0kUqnITm+29f5jBsi2uJ2Gxd6R2/8dWEulx5l/UdWJcA6PsmLhpBfRYwtANDE7U2ZfLEKgL8Itseny+qZiAV6+1RM/JtbHwmbDImgcnZc/oubvMNjgD2Bu/2qMNfh1Fnh0dImEgLs7jjguQ+6BO8FU47NBpUDU315pMXxG6R3wYyvRNmNxogvJpm2qBYB+G4oZDGleRHWwE6n7xyM4sHRIuA+n+eg0LBQfOENG/RCes+mQ==</Modulus><Exponent>AQAB</Exponent><P>0yPoNsAuQw7Iu6UaWETkbJuWdF6atJgEqx5hSN/2He3Wo6ePTeALb+KO97mLWjK8Qlx7C6sQncJvd30/McMWczWDC+r/6YPbrNTJ5J1Wf6ZkmhswTdiAYQC6mp+oHjG2LRgZxzfyWx0DkqmceWsN8lQbrr/E9+tFm4aRLvwokRc=</P><Q>/IA9Xz1IXahF09Fq3jMXvvU19NX1iA23sFUnH3TLTiVD5eBRDCyjfEtNIqE01xGmF5Zk3M8/d/ogOUtcRkZgHmemgnmPYOvQwbkK00MW+Zqc14QIWB8T/SYG7lEpDwm2NTirhzdqgTHwXPSH25an5EtNhu3t5hcOUQc4c24Dm88=</Q><DP>kQmaa3nKjZ/rPhMlLLtuWjjquld+WE4Q7uc4ltHaBCcsCVGB/FHKKXptETiwxC4T6lYS5CoHiQdWgLNVpL5Jfqykr5wnXWaJkYi+gz+deqR3ePDyVpdBYOEaTjscRlmJtiJXGuWQowwkNywx46opc2xZc8oh6NgCAdsHgkoPJ9M=</DP><DQ>xmtPzwOtVkvhc25heHLV+i1fVSK9a0i8TEjeEY4KJUKLdJ0tk0+QuR/DYa8kgs1Po8F/STVjPrjDkcJypJaw+bOh2W4FCbjFtzXeF93ktAiXr5QFqpnhb1DuqWAFz9dl47TAteUKQLQpmiJekGR3UqMKLTnTdLgxe+McrTiKhWk=</DQ><InverseQ>AvnOAtdvcflxZdH42b3MJ9rUNN58J1p5RJzKjYLGBAshTBDUjf8f7KMrttSZy7wnfxatn6GXSSK7SrOTpA9lfXxd89Oj2Ugzlry8QWkorQ8wFokT6ifyGFPI+dM/Oh1WpK+mRHxeFAPmeZVMq5u7EY4VZPNIyXlV7HG6+/SrfxM=</InverseQ><D>TBoPWpHR3NwVn+9eRHVhE0RgJT5eKecuo3Tj410sf21Rj8uKc+7OgVLwOib0TmyoeWj3cBGpXuLctkROzJdIzThREzu36g1yAXRSCOkbmk1WRf43HljT1Z2KfOReai0noEGT2/7n0uZqNMKI58++sGTezeggWGKch5vNjk0hRN1preV7J1zyubonSrnwwEC2PUluLzVRp4lyG5KEkSE/vSqBDHAsL6ZG+yS6XY/vIUDhiKZmgNswnzJVAWEElMNJoPP5dzG+HOQrn91XETekSJeTJ1FpeMg8s+ZPDSGpnzjbP3sTAaF+Kc0sEwaFS/EioMiupKRBKgXa+DYwQYYkJQ==</D></RSAKeyValue>";

        private static string Puk = "<RSAKeyValue><Modulus>0EEdKBvBFu2b1zhNmmIJu/eHGVmaTdq+G1ahZBiVh4zSYQHaFv7h3jcx9pAu63RiiRMcaapkuKEmGSx38ci1QSnqjWLg3ACDgqjofF9a0kUqnITm+29f5jBsi2uJ2Gxd6R2/8dWEulx5l/UdWJcA6PsmLhpBfRYwtANDE7U2ZfLEKgL8Itseny+qZiAV6+1RM/JtbHwmbDImgcnZc/oubvMNjgD2Bu/2qMNfh1Fnh0dImEgLs7jjguQ+6BO8FU47NBpUDU315pMXxG6R3wYyvRNmNxogvJpm2qBYB+G4oZDGleRHWwE6n7xyM4sHRIuA+n+eg0LBQfOENG/RCes+mQ==</Modulus><Exponent>AQAB</Exponent><P></P><Q></Q><DP></DP><DQ></DQ><InverseQ></InverseQ><D></D></RSAKeyValue>";
        /// <summary>
        /// 字符串加密
        /// </summary>
        /// <param name="enStr"></param>
        /// <returns></returns>
        public static string ToRsaEncrypt(this string enStr)
        {
            using (var rsa = RSA.Create())
            {
                rsa.FromXml(Puk);
                return Convert.ToBase64String(rsa.Encrypt(Encoding.UTF8.GetBytes(enStr), RSAEncryptionPadding.Pkcs1));
            }
        }
        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="deStr"></param>
        /// <returns></returns>
        public static string ToRsaDecrypt(this string deStr)
        {
            using (var rsa = RSA.Create())
            {
                rsa.FromXml(Prk);
                return Encoding.UTF8.GetString((rsa.Decrypt(Convert.FromBase64String(deStr), RSAEncryptionPadding.Pkcs1)));
            }
        }

        /// <summary>
        /// 创建实例 从xml中
        /// </summary>
        /// <param name="rsa"></param>
        /// <param name="xmlString">xml内容</param>
        public static void FromXml(this RSA rsa, string xmlString)
        {
            RSAParameters parameters = new RSAParameters();

            XmlDocument xmlDoc = new XmlDocument();

            //加载xml
            xmlDoc.LoadXml(xmlString);
            if (xmlDoc.DocumentElement.Name.Equals("RSAKeyValue"))
            {
                foreach (XmlNode node in xmlDoc.DocumentElement.ChildNodes)
                {
                    switch (node.Name)
                    {
                        case "Modulus": parameters.Modulus = (string.IsNullOrEmpty(node.InnerText) ? null : Convert.FromBase64String(node.InnerText)); break;
                        case "Exponent": parameters.Exponent = (string.IsNullOrEmpty(node.InnerText) ? null : Convert.FromBase64String(node.InnerText)); break;
                        case "P": parameters.P = (string.IsNullOrEmpty(node.InnerText) ? null : Convert.FromBase64String(node.InnerText)); break;
                        case "Q": parameters.Q = (string.IsNullOrEmpty(node.InnerText) ? null : Convert.FromBase64String(node.InnerText)); break;
                        case "DP": parameters.DP = (string.IsNullOrEmpty(node.InnerText) ? null : Convert.FromBase64String(node.InnerText)); break;
                        case "DQ": parameters.DQ = (string.IsNullOrEmpty(node.InnerText) ? null : Convert.FromBase64String(node.InnerText)); break;
                        case "InverseQ": parameters.InverseQ = (string.IsNullOrEmpty(node.InnerText) ? null : Convert.FromBase64String(node.InnerText)); break;
                        case "D": parameters.D = (string.IsNullOrEmpty(node.InnerText) ? null : Convert.FromBase64String(node.InnerText)); break;
                    }
                }
            }
            else
            {
                throw new Exception("Invalid XML RSA key.");
            }

            rsa.ImportParameters(parameters);
        }

        /// <summary>
        /// 获取公钥的内容
        /// //用于js端的
        /// </summary>
        /// <returns></returns>
        public static string GetPublicKeyString()
        {
            return RsaKeyConvert.PublicKeyXmlToPem(Puk);
        }
    }
}
